/// 获取数据处理状态
pub enum FetchMsg<T: serde::ser::Serialize> {
    FetchData,
    FetchReady(Result<T, anyhow::Error>), // 2.
    FetchError,
}

/// 请求的url的前缀
pub const URL_PREFIX: &'static str = "http://admin.army.rs/api/v1";

/// 调用布局文件
#[macro_export]
macro_rules! render_layout {
    ($path: expr) => ({
        include!(concat!("../templates/layouts", $path))
    })
}

/// 调用页面文件
#[macro_export]
macro_rules! render_view {
    ($path: expr) => ({
        include!(concat!("../../templates/pages", $path))
    })
}

#[macro_export]
macro_rules! fetch_callback {
    () => {
        move |res: yew::services::fetch::Response<yew::format::Json<Result<$crate::types::SwapData, anyhow::Error>>>| { // 2.
            let (meta, yew::format::Json(data)) = res.into_parts();
            return if meta.status.is_success() {
                $crate::web::FetchMsg::FetchReady(data)
            } else {
                $crate::web::FetchMsg::FetchError
            };
        }
    }
}

#[macro_export]
macro_rules! request_post {
    ($url: expr) => {{
        let url = format!("{}{}", $crate::web::URL_PREFIX, $url);
        yew::services::fetch::Request::post(&url)
            .header("content-type", "application/json;charset=UTF-8")
            .body(yew::format::Nothing)
            .unwrap()
    }};
    ($url: expr, $data: expr) => {{
        let url = format!("{}{}", $crate::web::URL_PREFIX, $url);
        let encrypt_data = $crate::crypt::encrypt_to_value($data);
        yew::services::fetch::Request::post(&url)
            .header("content-type", "application/json;charset=UTF-8")
            .body(yew::format::Json($data))
            .unwrap()
    }};
}

#[macro_export]
macro_rules! bind_data {
    ($rows: expr, [$($field: ident),+]) => {
        lucky::web::Query::bind(move |tbody: &web_sys::Element| {
            let mut content = String::new();
            for row in $rows {
                content.push_str("<tr>");
                $(content.push_str(&format!("<td>{}</td>", row.$field));)+
                content.push_str("<td>&nbsp;</td>");
                content.push_str("</tr>");
            }
            tbody.set_inner_html(&content);
        });
    }
}

pub mod console;
pub use console::Console;

pub mod html;
pub use html::Query;

pub mod local_storage;
pub use local_storage::LocalStorage;

pub mod api;
pub use api::{Api, ApiCached};

pub mod dialog;
pub use dialog::Dialog;

pub mod validation;
pub use validation::Validation;
